package com.uh.calculation.operator;

import com.uh.calculation.bean.*;
import com.uh.calculation.util.FormulaUtil;
import com.uh.calculation.util.GetBean;
import com.uh.calculation.util.GetFileds;
import com.uh.calculation.util.TimeUtil;
import com.uh.microservice.core.dao.JdbcDao;
import lombok.Data;
import org.omg.CORBA.LongLongSeqHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.lang.StrictMath.abs;

@Data
/**
 * 同比增速算子  即今年数据和上一周周期数据比较   (单表)
 */
public class ZsOperator {

    private JdbcDao jdbcDao;

    private WarnJiSuanBean warnJiSuanBean;

    public ZsOperator(JdbcDao jdbcDao, WarnJiSuanBean warnJiSuanBean) {
        this.jdbcDao = jdbcDao;
        this.warnJiSuanBean = warnJiSuanBean;

    }

    public void jisuan(List<Map<String, Object>> ls, List<OperatorVal> list) {
        if (ls == null || ls.size() == 0) {
            return;
        }
        //反转2021 与 2022字段区别
        //GetFileds getFileds = new GetFileds(jdbcDao,list,warnJiSuanBean);
        for (OperatorVal operatorVal : list) {
            List<FormulaBean> formulaBeans = operatorVal.getFormulaBeans();//计算公式
            String field = operatorVal.getField();
            List<TbFeild> tbFeilds = GetBean.getTbf(field);
            String f1 = tbFeilds.get(0).getFieldName();
            String f2 = tbFeilds.get(1).getFieldName();
            Date lastTime = warnJiSuanBean.getLastTime();
            for (int i = 0; i < ls.size(); i++) {
                try {
                    String timeCode = ls.get(i).get("time_code").toString();
                    if (TimeUtil.bj(lastTime, timeCode, warnJiSuanBean.getYear_is_month(), warnJiSuanBean.getIs_hc())) {
                        String val1 = ls.get(i).get(f1).toString();
                        String val2 = ls.get(i).get(f2).toString();
                        //根据当前计算类解析规则  将数据和字段对应
                        tbFeilds.get(0).setData(val1);
                        tbFeilds.get(1).setData(val2);
                        //扔进公式解析工具
                        new FormulaUtil(tbFeilds, formulaBeans, warnJiSuanBean, operatorVal.getDimension(), timeCode);
                    }
                } catch (Exception e) {
                    //e.printStackTrace();
                }
            }
        }
    }


}